const connection = require("../app/database")

class StashService {
    async create(name,chargeId,address,area,phone,capacity,remark) {
        const statement = `INSERT INTO stash (name,charge_id,address,area,phone,capacity,remark) VALUES (?,?,?,?,?,?,?);`
        const result = await connection.execute(statement,[name,chargeId,address,area,phone,capacity,remark])
        return result
    }
    async getListCount() {
        const statement = `SELECT COUNT(*) AS totalCount from stash;`
        const [result] = await connection.execute(statement)
        return result[0]
    }
    async getList(pageSize,offset) {     
        try {
            const statement = `select * from stash order by id desc limit ${pageSize} offset ${offset};`;
            const [result] = await connection.execute(statement);
            console.log("result",result);
            return result
        } catch (error) {
            console.log("error",error);
        }                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
    }
    async edit(id,name,phone,address,chargeId,area,capacity,remark) {
        const statement = `UPDATE stash SET 
                                name = ?,
                                phone = ?,
                                address = ?,
                                charge_id = ?,
                                area = ?,
                                capacity = ?,
                                remark = ?
                            WHERE id = ?;`
        const [result] = await connection.execute(statement,[name,phone,address,chargeId,area,capacity,remark,id]) 
        return result
    }
    async remove(id) {
        const statement = `DELETE FROM stash WHERE id = ?;`
        const result = await connection.execute(statement,[id])
        return result
    }
}

module.exports = new StashService()